;; Tests of the non-robust Ramer-Douglas-Peucker algorithm
;; Copyright (C) 2023  Rocks Mazama
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.compgeom.ramer-douglas-peucker-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.compgeom.ramer-douglas-peucker :refer [classic-rdp heap-rdp]]))

(defn- check-rdp
  ([points tests]
   (check-rdp classic-rdp points tests)
   (check-rdp heap-rdp points tests))
  ([rdp points tests]
   (is (= points (rdp points 0)))
   (doseq [[epsilon expected] tests] (is (= expected (rdp points epsilon))))))

(deftest small-test
  (testing "zero"
    (let [points []]
      (check-rdp points {})))
  (testing "one"
    (let [points [[-378 17]]]
      (check-rdp points {})))
  (testing "two"
    (let [points [[-378 17] [-1111 -123]]]
      (check-rdp points {}))))

(deftest bigger-test
  (testing "https://towardsdatascience.com/simplify-polylines-with-the-douglas-peucker-algorithm-ac8ed487a4a1"
    (let [points [[1 1] [1.3 2] [2 1.2] [3 1]]
          tests {0.5 [[1 1] [1.3 2] [3 1]]}]
      (check-rdp points tests)))
  (testing "https://medium.com/mlearning-ai/applications-of-ramer-douglas-peucker-algorithm-in-machine-learning-that-you-might-not-have-heard-63b0c4f15a43 FIRST"
    (let [points [[0.0, 0.0],[3.0, 8.0],[5.0, 2.0],
                  [5.0, 4.0],[6.0, 20.0],[6.4, 15.5],
                  [7.0, 25.0],[9.1, 16.9],[10.0, 10.0],
                  [11.0, 5.5], [17.3, 3.2], [27.8, 0.1]]
          tests {1 [[0.0, 0.0],[3.0, 8.0],[5.0, 2.0],
                    [7.0, 25.0], [11.0, 5.5], [27.8, 0.1]]}]
      (check-rdp points tests)))
  (testing "https://medium.com/mlearning-ai/applications-of-ramer-douglas-peucker-algorithm-in-machine-learning-that-you-might-not-have-heard-63b0c4f15a43 SHAMPOO"
    (let [points [[0,266.0]
                  [1,145.9]
                  [2,183.1]
                  [3,119.3]
                  [4,180.3]
                  [5,168.5]
                  [6,231.8]
                  [7,224.5]
                  [8,192.8]
                  [9,122.9]
                  [10,336.5]
                  [11,185.9]
                  [12,194.3]
                  [13,149.5]
                  [14,210.1]
                  [15,273.3]
                  [16,191.4]
                  [17,287.0]
                  [18,226.0]
                  [19,303.6]
                  [20,289.9]
                  [21,421.6]
                  [22,264.5]
                  [23,342.3]
                  [24,339.7]
                  [25,440.4]
                  [26,315.9]
                  [27,439.3]
                  [28,401.3]
                  [29,437.4]
                  [30,575.5]
                  [31,407.6]
                  [32,682.0]
                  [33,475.3]
                  [34,581.3]
                  [35,646.9]]
          tests {1.5 [[0,266.0]
                      [1,145.9]
                      [2,183.1]
                      [3,119.3]
                      [6,231.8]
                      [9,122.9]
                      [10,336.5]
                      [13,149.5]
                      [15,273.3]
                      [16,191.4]
                      [17,287.0]
                      [18,226.0]
                      [20,289.9]
                      [21,421.6]
                      [22,264.5]
                      [25,440.4]
                      [26,315.9]
                      [30,575.5]
                      [31,407.6]
                      [32,682.0]
                      [33,475.3]
                      [35,646.9]]}]
      (check-rdp points tests))))
